package com.dengdai.applibrary.widgets.custom;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.adapter.TopIndicatorAdapter;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.utils.BaseViewUtils;

import java.util.List;

/**
 * @说明 头部指示器
 * @作者 LY
 * @时间 16/5/26 08:23
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public class TopIndicatorView extends FrameLayout {
    private PagerSlidingTabStrip topIndicatorView;
    private CustomTabViewPager viewPager;
    private TopIndicatorAdapter topIndicatorAdapter;


    public TopIndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.TOP_INDICATOR_VIEW), null);
        topIndicatorView = (PagerSlidingTabStrip) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.TOPINDICATORVIEW));
        viewPager = (CustomTabViewPager) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.VIEWPAGER));
        topIndicatorView.setDividerColor(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.COLOR, ResIdName.TOPINDICATORDIVIDERCOLOR));
        topIndicatorView.setUnderlineColorResource(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.COLOR, ResIdName.TOP_INDICATOR_UNDERLINE_COLOR));
        topIndicatorView.setIndicatorColor(getResources().getColor(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.COLOR, ResIdName.TOP_INDICATOR_COLOR)));
        topIndicatorView.setIndicatorHeight(getResources().getDimensionPixelSize(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.DIMEN, ResIdName.TOP_INDICATOR_VIEW_HEIGHT)));
        topIndicatorView.setTextColor(getResources().getColor(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.COLOR, ResIdName.TOP_INDICATOR_TEXT_NORMAL_COLOR)));
        topIndicatorView.setTextSelectColor(getResources().getColor(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.COLOR, ResIdName.TOP_INDICATOR_TEXT_SELECT_COLOR)));
        topIndicatorView.setTextSize(getResources().getDimensionPixelSize(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.DIMEN, ResIdName.TOP_INDICATOR_TEXT_SIZE)));
        addView(view);
    }

    /**
     * 设置头部指示器
     *
     * @param fragmentManager
     * @param listTop         头部指示器数据
     * @param listContent     指示器数据源
     */
    public void initData(FragmentManager fragmentManager, List<String> listTop, List<Fragment> listContent, boolean isShouldExpand) {
        topIndicatorView.setShouldExpand(isShouldExpand);
        topIndicatorAdapter = new TopIndicatorAdapter(fragmentManager);
        topIndicatorAdapter.listTop.clear();
        topIndicatorAdapter.listTop.addAll(listTop);
        topIndicatorAdapter.listFragment.clear();
        topIndicatorAdapter.listFragment.addAll(listContent);
        viewPager.setOffscreenPageLimit(topIndicatorAdapter.listFragment.size() - 1);
        viewPager.setAdapter(topIndicatorAdapter);
        topIndicatorView.setViewPager(viewPager);
    }
}
